package com.useeinfo.oa.open.api.maintainOrder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Position;
import com.useeinfo.oa.modules.maintaining.biz.AuditOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainFocusBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.entity.AuditOrder;
import com.useeinfo.oa.modules.maintaining.entity.MaintainFocus;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrder;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/maintainDashboardReport")
public class MaintainDashboardReportAPI {
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private MaintainOrderBiz maintainOrderBiz;
    @Autowired
    private MaintainFocusBiz maintainFocusBiz;
    @Autowired
    private AuditOrderBiz auditOrderBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;

    /**
     * 正在维修中的设备
     */
    @RequestMapping(value = "/maintainFacilityCount")
    public JSONObject maintainFacilityCount(@RequestParam(value = "enterpriseId", required = false) String enterpriseId){
        JSONObject jsonObject = new JSONObject();
        if(enterpriseId==null){
            return jsonObject;
        }
        //获取下级企业ids（包括自己）
        String ids = enterpriseBiz.childrenIds(enterpriseId);
        JSONArray rows = maintainOrderBiz.maintainFacilityCount(ids);
        jsonObject.put("rows",rows);
        return jsonObject;
    }


    /**
     *
     */
    @RequestMapping(value = "/getOrderList")
    public JSONObject getOrderList(@RequestParam(value = "userId", required = false) String userId,
                                  @RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                  @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                  @RequestParam(value = "enterpriseId", required = false) String enterpriseId,
                                  @RequestParam(value = "type", required = false) String type){
        JSONObject data = new JSONObject();
        data.put("records",0);
        JSONArray rows = new JSONArray();
        data.put("rows",rows);
        if(StringUtils.isBlank(type)){
            return data;
        }
        SystemUser user = systemUserBiz.findModel(StringConverters.ToLong(userId));
        Enterprise enterprise = enterpriseBiz.findModel(StringConverters.ToLong(enterpriseId));
        if(user==null||enterprise==null){
            return data;
        }
        //查关注的工单
        if("focus".equals(type)){
            QueryParam queryParamF = new QueryParam(pageIndexParam,pageSizeParam);
            queryParamF.getSqlMap().put("systemUserId",userId);
            queryParamF.getSqlMap().put("enterpriseId",enterpriseId);
            List<MaintainFocus> maintainFocusList = maintainFocusBiz.findList(queryParamF);
            if(CollectionUtils.isNotEmpty(maintainFocusList)){
                for(MaintainFocus maintainFocus : maintainFocusList){
                    if(maintainFocus.getMaintainOrder()!=null){
                        MaintainOrder maintainOrder = maintainFocus.getMaintainOrder();
                        JSONObject json = maintainOrder.toListJSONObject();
                        JSONArray tags = maintainOrderBiz.getTags(maintainOrder,user,null);
                        json.put("tags",tags);
                        rows.add(json);
                    }
                }
            }
            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("systemUserId",userId);
            queryParam.getSqlMap().put("enterpriseId",enterpriseId);
            Long records = maintainFocusBiz.totalRecord(queryParam.getSqlMap());
            if(records>0){
                data.put("records",records);
            }
        }
        //查发起的工单
        if("create".equals(type)){
            QueryParam queryParamC = new QueryParam(pageIndexParam,pageSizeParam);
            queryParamC.getSqlMap().put("systemUserId",userId);
            queryParamC.getSqlMap().put("enterpriseId",enterpriseId);
            List<MaintainOrder> maintainOrderList = maintainOrderBiz.findList(queryParamC);
            if(CollectionUtils.isNotEmpty(maintainOrderList)){
                for(MaintainOrder maintainOrder : maintainOrderList){
                    JSONObject json = maintainOrder.toListJSONObject();
                    JSONArray tags = maintainOrderBiz.getTags(maintainOrder,user,null);
                    json.put("tags",tags);
                    rows.add(json);
                }
            }
            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("systemUserId",userId);
            queryParam.getSqlMap().put("enterpriseId",enterpriseId);
            Long records = maintainOrderBiz.totalRecord(queryParam.getSqlMap());
            if(records>0){
                data.put("records",records);
            }
        }
        //查我接单的
        if("receive".equals(type)){
            QueryParam queryParamR = new QueryParam(pageIndexParam,pageSizeParam);
            queryParamR.getSqlMap().put("receiveUserId",userId);
            queryParamR.getSqlMap().put("enterpriseId",enterpriseId);
            List<MaintainOrder> maintainOrderList = maintainOrderBiz.findList(queryParamR);
            if(CollectionUtils.isNotEmpty(maintainOrderList)){
                for(MaintainOrder maintainOrder : maintainOrderList){
                    JSONObject json = maintainOrder.toListJSONObject();
                    JSONArray tags = maintainOrderBiz.getTags(maintainOrder,user,null);
                    json.put("tags",tags);
                    rows.add(json);
                }
            }
            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("receiveUserId",userId);
            queryParam.getSqlMap().put("enterpriseId",enterpriseId);
            Long records = maintainOrderBiz.totalRecord(queryParam.getSqlMap());
            if(records>0){
                data.put("records",records);
            }
        }
        //我审核的
        if("audit".equals(type)){
            Position position = user.getPosition(enterprise,user.getPositionSet());
            QueryParam queryParamA = new QueryParam(pageIndexParam,pageSizeParam);
            queryParamA.getSqlMap().put("auditUserId",userId);
            queryParamA.getSqlMap().put("enterpriseId",enterpriseId);
            // 添加搜索审核状态  审批中
            queryParamA.getSqlMap().put("auditIn","auditIn");
            if(position!=null){
                queryParamA.getSqlMap().put("auditPositionId",position.getId()+"");
            }
            List<MaintainOrder> maintainOrderList = maintainOrderBiz.findList(queryParamA);
            if(CollectionUtils.isNotEmpty(maintainOrderList)){
                for(MaintainOrder maintainOrder : maintainOrderList){
                    JSONObject json = maintainOrder.toListJSONObject();
                    JSONArray tags = maintainOrderBiz.getTags(maintainOrder,user,null);
                    json.put("tags",tags);
                    rows.add(json);
                }
            }
            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("auditUserId",userId);
            queryParam.getSqlMap().put("enterpriseId",enterpriseId);
            if(position!=null){
                queryParam.getSqlMap().put("auditPositionId",position.getId()+"");
            }
            Long records = maintainOrderBiz.totalRecord(queryParam.getSqlMap());
            if(records>0){
                data.put("records",records);
            }
        }
        //待接单的
        if("waitTaking".equals(type)){
            ResultDto resultDto = maintainOrderBiz.list("0",pageIndexParam,pageSizeParam,user,enterprise,null,null);
            JSONObject jsonObject = JSONObject.parseObject(resultDto.getData().toString());
            rows = jsonObject.getJSONArray("rows");
            ResultDto resultDto1 = maintainOrderBiz.list("0","-1","-1",user,enterprise,null,null);
            JSONObject jsonObject1 = JSONObject.parseObject(resultDto1.getData().toString());
            JSONArray array = jsonObject1.getJSONArray("rows");
            if(array.size()>0){
                data.put("records",array.size());
            }
        }
        data.put("rows",rows);
        return data;
    }
}
